<template>
  <a-tree-select
    v-if="treeData.length"
    v-model="curVal"
    :treeData="treeData"
    :replaceFields="replaceFields"
    @select="onSelect"
    v-bind="$attrs"
    :defaultExpandAll="true"
  ></a-tree-select>
</template>

<script>
export default {
  components: {},
  props: {
    bomId: String,
    value: String
  },
  data() {
    return {
      curVal: undefined,
      treeData: [],
      replaceFields: { children: 'Children', title: 'Name', key: 'Id', value: 'Id' }
    }
  },
  mounted() {
    this.curVal = this.value
    this.reloadData()
  },
  watch: {
    value(newVal, oldVal) {
      this.curVal = newVal
    }
  },
  methods: {
    onSelect(value, node, extra) {
      this.$emit('input', value)
      this.$emit('select', value, node.dataRef, extra)
    },
    reloadData() {
      this.$http.get(`/api/MaterialBomTree/GetTree?bomId=${this.bomId}`).then((result) => {
        this.treeData = result.Data
      })
    }
  }
}
</script>

<style>
</style>
